﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MedicalManagementSystem.Utils;
using MedicalManagementSystem.ViewModels.Reports;
using MedicalManagementSystem.Helpers;

namespace MedicalManagementSystem.Controllers
{
    public class ReportsController : GenericController
    {
        //
        // GET: /Report/

        public ActionResult Index()
        {
            //Menu.ID = "anal, index";
            return RedirectToAction("StatisticsReport");
        }
        [EnumAuthorize(Roles = UserRoles.StatisticRole)]
        public ActionResult StatisticsReport()
        {
            Menu.ID = "anal,statReport";
            return View(new StatisticsReportModel());
        }
        [EnumAuthorize(Roles = UserRoles.StatisticRole)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult StatisticsReport(StatisticsReportModel model)
        {
            try
            {
                var report = new StatisticsReport(model.BeginDate, model.EndDate);
                var stream = new MemoryStream();
                report.Generate(stream);
                stream.Flush();
                stream.Seek(0, SeekOrigin.Begin);
                var fileResult = new FileStreamResult(stream, "application/vnd.xls") { FileDownloadName = "StatisticsReport.xls" };
                return fileResult;
            }
            catch (Exception e)
            {
               // AddGeneralError(e);
            }
            return RedirectToAction("StatisticsReport");
        }

    }
}
